package to52.android.screens;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import to52.android.adapters.CapteurDetailsAdapter;
import to52.android.adapters.PatientDetailsAdapter;
import to52.android.database.DatabaseModel;
import to52.android.database.DatabaseSimulator;
import to52.android.database.RetreiveData;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.androidplot.series.XYSeries;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;

import fr.android.ui.SliceNavigator;
import fr.utbm.Patient;
import fr.utbm.Sensor;

public class PlotView extends SliceNavigator {

	private List<Sensor> capteurs;
	private int currentIndex;
	
	private ListView currentCapteurDetailsView;
	
	private Patient currentPatient;
	
	@Override
	public void afterCreate() {

		currentCapteurDetailsView = (ListView) findViewById(R.id.navigation_item_list);
		
		//DatabaseModel dm = DatabaseSimulator.getInstance();
		RetreiveData rd = new RetreiveData(this);
		
		capteurs = new ArrayList<Sensor>();
		currentPatient = rd.getPatient(getIntent().getExtras().getInt("current_patient_id"));
		capteurs = currentPatient.getSensors();
		
		rd.close();
		
		Integer c = getIntent().getExtras().getInt("capteur_id");
		
		// convert ID to index
		int realIndex = 0;
		for (int i=0; i<capteurs.size(); ++i) {
			if (capteurs.get(i).getType().getID() == c) {
				realIndex = i;
			}
		}
		
		setCurrentCapteur(realIndex);
	}

	
	private void setCurrentCapteur(int index) {
		currentIndex = index;
		
		Sensor currentCapteur = capteurs.get(index);
		setTitle(currentCapteur.getType().getLabel() + " de " + currentPatient.getName());
		currentCapteurDetailsView.setAdapter(new CapteurDetailsAdapter(getApplicationContext(), currentPatient, currentCapteur)); 
		
		setSliceLeftEnable(currentIndex != 0);
		setSliceRightEnable(currentIndex != capteurs.size() - 1);
	}
	
	
	@Override
	public void onSliceLeft() {
		playLeftAnimation();
		setCurrentCapteur(currentIndex - 1);
	}

	@Override
	public void onSliceRight() {
		playRightAnimation();
		setCurrentCapteur(currentIndex + 1);
	}


}

